//
//  GKZYHomeCell.m
//  GKZY_CLASS
//
//  Created by KwokChun Cheung on 15/11/26.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "GKZYHomeTermCell.h"
#import "xxptmobile.h"

#pragma mark - OOHomeTermModel
@implementation GKZYHomeTermModel
@end

#pragma mark - OOHomeTermCell
@interface GKZYHomeTermCell()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *dateRangeLabel;
@property (nonatomic, strong) UILabel *checkFlagLabel;

@property (nonatomic, assign) BOOL checked;
@end

@implementation GKZYHomeTermCell{
    CGFloat checkSizeUnit;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        checkSizeUnit = 25;
        
        [self addGapLineWith:self on:LinePositionBottom];
        
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(20);
        }];
        
        [self addSubview:self.checkFlagLabel];
        [self.checkFlagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-10);
            make.size.mas_equalTo(CGSizeMake(checkSizeUnit, checkSizeUnit));
        }];
    }
    return self;
}

#pragma mark - Setter For Property

-(void)setModel:(GKZYHomeTermModel *)model{
    _model = model;
    if (model) {
        self.checked = model.checked;
        self.titleLabel.text = model.title;
        self.dateRangeLabel.text = model.dateRange;
    }
}

-(void)setChecked:(BOOL)checked{
    _checked = checked;
    if (checked) {
        self.checkFlagLabel.backgroundColor = [UIColor colorWithRed:71/255.0 green:150/255.0 blue:239/255.0 alpha:1];
        self.checkFlagLabel.text = @"\U0000F00C";
    }else{
        self.checkFlagLabel.backgroundColor = [UIColor whiteColor];
        self.checkFlagLabel.text = @"";
    }
}

#pragma mark - Getter For UI

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel *label = [UILabel new];
            label.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
            label.font = [UIFont systemFontOfSize:16];
            label.numberOfLines = 0;
            label;
        });
    }
    return _titleLabel;
}

-(UILabel *)dateRangeLabel{
    if (!_dateRangeLabel) {
        _dateRangeLabel = ({
            UILabel *label = [UILabel new];
            label.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
            label.font = [UIFont systemFontOfSize:8];
            label;
        });
    }
    return _dateRangeLabel;
}

-(UILabel *)checkFlagLabel{
    if (!_checkFlagLabel) {
        _checkFlagLabel = ({
            UILabel *label = [UILabel new];
            label.layer.cornerRadius = checkSizeUnit/2;
            label.layer.borderColor = [UIColor lightGrayColor].CGColor;
            label.layer.borderWidth = 0.5f;
            label.layer.masksToBounds = YES;
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [[ResourceHelper archiveFont:FONT_FONTAWESOME withType:@"otf"] fontWithSize:18];
            label;
        });
    }
    return _checkFlagLabel;
}

#pragma mark - Tools Methods

typedef NS_ENUM(NSUInteger, LinePosition) {
    LinePositionTop,
    LinePositionLeft,
    LinePositionRight,
    LinePositionBottom,
};
-(void)addGapLineWith:(UIView *)view on:(LinePosition)posit{
    //用于防止重复插入
    NSInteger tagNumber = 960;
    switch (posit) {
        case LinePositionTop:
            tagNumber = 961;
            break;
        case LinePositionLeft:
            tagNumber = 962;
            break;
        case LinePositionRight:
            tagNumber = 963;
            break;
        case LinePositionBottom:
            tagNumber = 964;
            break;
        default:
            break;
    }
    UIView *exist = [view viewWithTag:tagNumber];
    if (exist) { return; }
    
    CGFloat lineSizeUnit = 0.5;
    UILabel *line = [UILabel new];
    line.backgroundColor = [UIColor lightGrayColor];
    line.tag = tagNumber;
    
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        if (posit==LinePositionRight || posit==LinePositionLeft) {
            if (posit==LinePositionRight) {
                make.right.equalTo(view);
            }else if (posit==LinePositionLeft){
                make.left.equalTo(view);
            }
            make.top.equalTo(view);
            make.height.equalTo(view);
            make.width.equalTo(@(lineSizeUnit));
        }else if (posit==LinePositionTop || posit==LinePositionBottom){
            if (posit==LinePositionTop) {
                make.top.equalTo(view);
            }else if (posit==LinePositionBottom){
                make.bottom.equalTo(view);
            }
            make.height.equalTo(@(lineSizeUnit));
            make.width.equalTo(view);
        }
    }];
}
@end
